package leetcode.templates.presum;

public class MaxPreSums1 {

	int[] nums;
	int len;
	long[] preSums;

	public MaxPreSums1(int[] nums) {
		this.nums = nums;
		this.len = nums.length;
		this.preSums = new long[len + 1];
	}

	public long[] preSum() {
		preSums[0] = nums[0];
		for (int i = 1; i < len; i++) {
			preSums[i] = preSums[i - 1] + nums[i];
		}
		return preSums;
	}

	public long preSum(int x, int y) {
		return x == 0 ? preSums[y] : preSums[y] - preSums[x - 1];
	}

	// 老老实实用long，计算的时候注意不要混着int做，可能出错
	public long[] sums(int[] nums) {
		int len = nums.length;
		long[] sums = new long[len];
		sums[0] = nums[0];
		for (int i = 1; i < len; i++) {
			sums[i] = sums[i - 1] + nums[i];
		}
		return sums;
	}

	// [L,R]之间的和
	public long sumsLR(long[] sums, int L, int R) {
		return L == 0 ? R < 0 ? 0 : sums[R] : sums[R] - sums[L - 1];
	}

	public long[] presum(int[] nums) {
		int len = nums.length;
		long[] sums = new long[len + 1];
		for (int i = 1; i < sums.length; i++) {
			sums[i] = sums[i - 1] + nums[i - 1];
		}
		return sums;
	}

	// [indexL,indexR]
	public long sumIndex(long[] sums, int indexL, int indexR) {
		return sums[indexR + 1] - sums[indexL];
	}

}
